package com.collins.thread.practices.ticket;

public class TicketSynchonizedRunnable implements Runnable {
    private static int ticket = 0;

    @Override
    public void run() {
        while (true) {
            if (sell()) {
                break;
            }
        }

    }

    private synchronized boolean sell() {
        if (ticket == 1000) {
            return true;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        ticket++;
        System.out.println(Thread.currentThread().getName() + " sells No." + ticket);
        return false;
    }
}
